package com.agileboot.admin.controller.exam;

import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.domain.bank.db.QuestionBank;
import com.agileboot.domain.bank.db.QuestionBankService;
import com.agileboot.domain.bank.query.ExamQuery;
import com.agileboot.domain.exam.db.ExamService;
import com.agileboot.domain.exam.vo.AddExamByBankVo;
import com.agileboot.domain.exam.vo.AddExamByQuestionVo;
import com.agileboot.domain.examrecord.db.ExamRecord;
import com.agileboot.domain.examrecord.db.ExamRecordService;
import com.agileboot.domain.question.db.Question;
import com.agileboot.domain.question.db.QuestionService;
import com.agileboot.domain.question.vo.QuestionVo;
import com.agileboot.domain.system.user.db.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;
import java.util.List;

/**
 * @author lx
 * @implNote 2022/2/26
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@Api(tags = "老师权限相关的接口")
@RequestMapping(value = "/teacher")
public class TeacherController {

//    private final OSSUtil ossUtil;

    private final ExamService examService;

    private final SysUserService userService;

    private final QuestionService questionService;

    private final ExamRecordService examRecordService;

    private final QuestionBankService questionBankService;

    @PostMapping("/getQuestionBank")
    @ApiOperation("获取所有题库信息")
    public ResponseDTO<PageDTO<QuestionBank>> getQuestionBankPage(@RequestBody ExamQuery examQuery) {
        return ResponseDTO.ok(questionBankService.getAllQuestionBanks(examQuery));
    }

//    @GetMapping("/getQuestion")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "questionType", value = "问题类型", dataType = "string", paramType = "query"),
//            @ApiImplicitParam(name = "questionBank", value = "问题所属题库", dataType = "string", paramType = "query"),
//            @ApiImplicitParam(name = "questionContent", value = "问题内容", dataType = "string", paramType = "query"),
//            @ApiImplicitParam(name = "pageNo", value = "页面数", required = true, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int", paramType = "query")
//    })
//    @ApiOperation("获取题目信息,可分页 ----> 查询条件(可无)(questionType,questionBank,questionContent),必须有的(pageNo,pageSize)")
//    public PageDTO<Question> getQuestion(@RequestParam(required = false) String questionType,
//                                                            @RequestParam(required = false) String questionBank,
//                                                            @RequestParam(required = false) String questionContent,
//                                                            Integer pageNo, Integer pageSize) {
//        return questionService.getQuestion(questionType, questionBank, questionContent, pageNo, pageSize);
//    }

    @GetMapping("/deleteQuestion")
    @ApiOperation("根据id批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "questionIds", value = "问题id的字符串以逗号分隔", required = true, dataType = "string", paramType = "query")
    })
    public void deleteQuestion(String questionIds) {
        questionService.deleteQuestionByIds(questionIds);
    }



    @GetMapping("/removeBankQuestion")
    @ApiOperation("将问题从题库移除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "questionIds", value = "问题id的字符串以逗号分隔", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "banks", value = "题库id的字符串以逗号分隔", required = true, dataType = "string", paramType = "query")
    })
    public void removeBankQuestion(String questionIds, String banks) {
        questionBankService.removeBankQuestion(questionIds, banks);
    }

//    @PostMapping("/uploadQuestionImage")
//    @ApiOperation("接受前端上传的图片,返回上传图片地址")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "file", value = "图片文件", required = true, dataType = "file", paramType = "body")
//    })
//    public CommonResult<String> uploadQuestionImage(MultipartFile file) throws Exception {
//        log.info("开始上传文件: {}", file.getOriginalFilename());
//        return CommonResult.<String>builder()
//                .data(ossUtil.picOSS(file))
//                .message("上传成功")
//                .build();
//    }

//    @PostMapping("/addQuestion")
//    @ApiOperation("添加试题")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "questionVo", value = "问题的vo视图对象", required = true, dataType = "questionVo", paramType = "body")
//    })
//    public String addQuestion(@RequestBody @Valid QuestionVo questionVo) {
//        questionService.addQuestion(questionVo);
//        return "新增成功";
//    }

//    @PostMapping("/updateQuestion")
//    @ApiOperation("更新试题")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "questionVo", value = "问题的vo视图对象", required = true, dataType = "questionVo", paramType = "body")
//    })
//    public String updateQuestion(@RequestBody @Valid QuestionVo questionVo) {
//        questionService.updateQuestion(questionVo);
//        return "修改成功";
//    }

    @GetMapping("/deleteQuestionBank")
    @ApiOperation("删除题库并去除所有题目中的包含此题库的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "删除题库的id字符串逗号分隔", required = true, dataType = "string", paramType = "query")
    })
    public String deleteQuestionBank(String ids) {
        questionBankService.deleteQuestionBank(ids);
        return "删除成功";
    }

    @PostMapping("/addQuestionBank")
    @ApiOperation("添加题库信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "questionBank", value = "题库的实体对象", required = true, dataType = "questionBank", paramType = "body")
    })
    public String addQuestionBank(@RequestBody QuestionBank questionBank) {
        questionBankService.addQuestionBank(questionBank);
        return "添加成功";
    }

    @GetMapping("/operationExam/{type}")
    @ApiOperation("操作考试的信息表(type 1启用 2禁用 3删除)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "操作类型", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "ids", value = "操作的考试id集合", required = true, dataType = "string", paramType = "query")
    })
    public String operationExam(@PathVariable("type") Integer type, String ids) {
        examService.operationExam(type, ids);
        return "操作成功";
    }

    @PostMapping("/addExamByBank")
    @ApiOperation("根据题库添加考试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addExamByBankVo", value = "根据题库添加考试vo对象", required = true, dataType = "addExamByBankVo", paramType = "body")
    })
    public String addExamByBank(@RequestBody @Valid AddExamByBankVo addExamByBankVo) {
        examService.addExamByBank(addExamByBankVo);
        return "添加成功";
    }

    @PostMapping("/addExamByQuestionList")
    @ApiOperation("根据题目列表添加考试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addExamByQuestionVo", value = "通过题目列表添加考试的vo对象", required = true, dataType = "addExamByQuestionVo", paramType = "body")
    })
    public String addExamByQuestionList(@RequestBody @Valid AddExamByQuestionVo addExamByQuestionVo) {
        examService.addExamByQuestionList(addExamByQuestionVo);
        return "添加成功";
    }

    @PostMapping("/updateExamInfo")
    @ApiOperation("更新考试的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addExamByQuestionVo", value = "通过题目列表添加考试的vo对象", required = true, dataType = "addExamByQuestionVo", paramType = "body")
    })
    public String updateExamInfo(@RequestBody AddExamByQuestionVo addExamByQuestionVo) {
        examService.updateExamInfo(addExamByQuestionVo);
        return "更新成功";
    }

    @GetMapping("/getExamRecord")
    @ApiOperation("获取考试记录信息,(pageNo,pageSize)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examId", value = "考试id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "页面数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int", paramType = "query")
    })
    public PageDTO<ExamRecord> getExamRecord(@RequestParam(required = false) Integer examId, Integer pageNo, Integer pageSize) {
        return examRecordService.getExamRecord(examId, pageNo, pageSize);
    }

    @GetMapping("/setObjectQuestionScore")
    @ApiOperation("设置考试记录的客观题得分,设置总分为逻辑得分+客观题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "totalScore", value = "总成绩", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "examRecordId", value = "考试记录id", required = true, dataType = "int", paramType = "query")
    })
    public ResponseDTO setObjectQuestionScore(Integer totalScore, Integer examRecordId) {
        examRecordService.setObjectQuestionScore(totalScore, examRecordId);
        return ResponseDTO.ok("成功");
    }

    @GetMapping("/getExamPassRate")
    @ApiOperation("提供每一门考试的通过率数据(echarts绘图)")
    public List<String> getExamPassRate() {
        return examService.getExamPassRateEchartData();
    }

    @GetMapping("/getExamNumbers")
    @ApiOperation("提供每一门考试的考试次数(echarts绘图)")
    public List<String> getExamNumbers() {
        return examService.getExamNumbersEchartData();
    }
}
